package top.huahaizhi.zhihu.daily.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import top.huahaizhi.zhihu.daily.BaseActivity;
import top.huahaizhi.zhihu.daily.K;
import top.huahaizhi.zhihu.daily.R;
import top.huahaizhi.zhihu.daily.adapter.StoryAdapter;
import top.huahaizhi.zhihu.daily.adapter.TopStoryAdapter;
import top.huahaizhi.zhihu.daily.bean.LatestNewsBean;
import top.huahaizhi.zhihu.daily.util.NetUtils;

public class ContentActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.vp_content_topStores)
    ViewPager topStoriesView;
    @BindView(R.id.rv_content_stories)
    RecyclerView storiesView;

    private StoryAdapter mStoreAdapter;
    private List<LatestNewsBean.StoriesBean> storiesBeen;
    private TopStoryAdapter mTopAdapter;
    private List<LatestNewsBean.TopStoriesBean> topStoriesBean;

    private boolean isActivityDestroy = false;

    private Handler changeTopPageHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if(isActivityDestroy)
                return false;
            if(topStoriesView!=null){
                int count = message.what;
                topStoriesView.setCurrentItem(count);
            }
            changeTopPageHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(topStoriesView!=null) {
                        int message;
                        if (topStoriesView.getCurrentItem() == mTopAdapter.getCount() - 1)
                            message = 0;
                        else
                            message = topStoriesView.getCurrentItem()+1;
                        changeTopPageHandler.sendEmptyMessage(message);
                    }
                }
            },5000);
            return false;
        }
    });


    @Override
    public int getLayoutId(Bundle savedInstanceState) {
        return R.layout.activity_content;
    }

    @Override
    protected void initData() {
        super.initData();
        storiesBeen = new ArrayList<>();
        topStoriesBean = new ArrayList<>();
        mStoreAdapter = new StoryAdapter(this, R.layout.item_content, storiesBeen);
        mTopAdapter = new TopStoryAdapter(this,topStoriesBean);
    }

    @Override
    protected void setParams() {
        super.setParams();
        storiesView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        storiesView.setAdapter(mStoreAdapter);
        topStoriesView.setAdapter(mTopAdapter);
        mStoreAdapter.setOnItemClickListener(this);
        changeTopPageHandler.sendEmptyMessageDelayed(1,5000);
    }

    @Override
    protected void accessNet() {
        super.accessNet();
        NetUtils.getInstance(this).requestNews(new NetUtils.OnRequestListener<LatestNewsBean>() {
            @Override
            public void onSuccess(LatestNewsBean object) {
                storiesBeen.clear();
                storiesBeen.addAll(object.getStories());
                mStoreAdapter.notifyDataSetChanged();

                topStoriesBean.clear();
                topStoriesBean.addAll(object.getTop_stories());
                mTopAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(String reason) {
                Toast.makeText(ContentActivity.this, reason, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        LatestNewsBean.StoriesBean item = (LatestNewsBean.StoriesBean) adapter.getItem(position);
        assert item != null;
        goDetail(item.getId(),item.getTitle());
    }

    private void goDetail(int id, String title){
        startActivity(new Intent(this,DetailActivity.class).putExtra(K.STORY_ID_KEY,id).putExtra(K.STORY_TITLE_KEY,title));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isActivityDestroy = true;
        changeTopPageHandler = null;

    }
}
